#include "fontconfigdialog.h"

#include <QFontComboBox>
#include <QHBoxLayout>
#include <QIcon>
#include <QSpinBox>

CFontConfigDialog::CFontConfigDialog(QWidget *parent)
    : QWidget(parent)
    , m_FontSizeWdiget(this)
    , m_fontFamilyComboBox(this)
    , m_BoldButton(QIcon("://image/font_bold"), "", "加粗", this)
    , m_ItalicButton(QIcon("://image/font_italic"), "", "斜体", this)
    , m_UnderlineButton(QIcon("://image/underline"), "", "下划线", this)
    , m_StrikeOutButton(QIcon("://image/strikeout"), "", "删除线", this)
{
    Init();
}

void CFontConfigDialog::SetFont(QFont font)
{
    m_font = font;
    m_FontSizeWdiget.setValue(font.pointSize());
    if (font.bold())
    {
        m_BoldButton.setChecked(true);
    }
    if (font.italic())
    {
        m_ItalicButton.setChecked(true);
    }
    if (font.underline())
    {
        m_UnderlineButton.setChecked(true);
    }
    if (font.strikeOut())
    {
        m_StrikeOutButton.setChecked(true);
    }
}

void CFontConfigDialog::FontChangedSlot(const QFont &font)
{
    m_font.setFamily(font.family());
    emit FontChanged(m_font);
}

void CFontConfigDialog::FontPointSizeChangedSlot(int val)
{
    m_font.setPointSize(val);
    emit FontChanged(m_font);
}

void CFontConfigDialog::FontBoldChanged(bool isChecked)
{
    m_font.setBold(isChecked);
    emit FontChanged(m_font);
}

void CFontConfigDialog::FontItalicChanged(bool isChecked)
{
    m_font.setItalic(isChecked);
    emit FontChanged(m_font);
}

void CFontConfigDialog::FontUnderlineChanged(bool isChecked)
{
    m_font.setUnderline(isChecked);
    emit FontChanged(m_font);
}

void CFontConfigDialog::FontStrikeoutChanged(bool isChecked)
{
    m_font.setStrikeOut(isChecked);
    emit FontChanged(m_font);
}

void CFontConfigDialog::Init()
{
    QVBoxLayout *pVLayout = new QVBoxLayout();
    setLayout(pVLayout);
    pVLayout->setObjectName("pVLayout");
    QMargins vlayoutMargins = pVLayout->contentsMargins();
    vlayoutMargins.setTop(3);
    pVLayout->setContentsMargins(vlayoutMargins);
    QHBoxLayout *pFontFamilyLayout = new QHBoxLayout();
    pFontFamilyLayout->setObjectName("pFontFamilyLayout");
    pFontFamilyLayout->setContentsMargins(0, 0, 0, 0);
    pVLayout->addItem(pFontFamilyLayout);
    //字体Family
    m_fontFamilyComboBox.currentFontChanged(m_font);
    m_fontFamilyComboBox.setEditable(false);
    m_fontFamilyComboBox.setObjectName("m_fontFamilyComboBox");
    connect(&m_fontFamilyComboBox, &QFontComboBox::currentFontChanged, this, &CFontConfigDialog::FontChangedSlot);
    pFontFamilyLayout->addWidget(&m_fontFamilyComboBox);

    //字体粗细 (Weight)
//    QSpinBox *pFontWidth = new QSpinBox(this);
//    pFontWidth->setObjectName("pFontWidth");
//    pFontFamilyLayout->addWidget(pFontWidth);

    //加粗，斜体，下划线等属性
    QHBoxLayout *pFontPropertyLayout = new QHBoxLayout();
    pFontPropertyLayout->setObjectName("pFontPropertyLayout");
    pFontPropertyLayout->setContentsMargins(0, 0, 0, 0);
    pVLayout->addItem(pFontPropertyLayout);
    //字体加粗
    m_BoldButton.setObjectName("m_BoldButton");
    connect(&m_BoldButton, &QToolButton::toggled, this, &CFontConfigDialog::FontBoldChanged);
    m_BoldButton.setCheckable(true);
    m_BoldButton.setFixedSize(25, 25);
    pFontPropertyLayout->addWidget(&m_BoldButton);
    //斜体
    m_ItalicButton.setObjectName("m_ItalicButton");
    connect(&m_ItalicButton, &QToolButton::toggled, this, &CFontConfigDialog::FontItalicChanged);
    m_ItalicButton.setCheckable(true);
    m_ItalicButton.setFixedSize(25, 25);
    pFontPropertyLayout->addWidget(&m_ItalicButton);
    //下划线Underline
    m_UnderlineButton.setObjectName("m_UnderlineButton");
    connect(&m_UnderlineButton, &QToolButton::toggled, this, &CFontConfigDialog::FontUnderlineChanged);
    m_UnderlineButton.setFixedSize(25, 25);
    m_UnderlineButton.setCheckable(true);
    pFontPropertyLayout->addWidget(&m_UnderlineButton);
    //删除线 (StrikeOut)
    m_StrikeOutButton.setObjectName("m_StrikeOutButton");
    connect(&m_StrikeOutButton, &QToolButton::toggled, this, &CFontConfigDialog::FontStrikeoutChanged);
    m_StrikeOutButton.setCheckable(true);
    m_StrikeOutButton.setFixedSize(25, 25);
    pFontPropertyLayout->addWidget(&m_StrikeOutButton);

    //Size
    //m_pFontSize = new QSpinBox(this);
    m_FontSizeWdiget.setObjectName("m_FontSizeWdiget");
    m_FontSizeWdiget.setValue(m_font.pointSize());
    connect(&m_FontSizeWdiget, QOverload<int>::of(&QSpinBox::valueChanged), this, &CFontConfigDialog::FontPointSizeChangedSlot);
    pFontPropertyLayout->addWidget(&m_FontSizeWdiget);
}
